www.gusucode.com > VC++ StuManage学生档案管理系统Access版-源码程序 > VC++ StuManage学生档案管理系统Access版-源码程序/code/PhotoDlg.cpp

    //Download by http://www.NewXing.com
// PhotoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StuManage.h"
#include "PhotoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPhotoDlg dialog
extern CStuManageApp theApp;

CPhotoDlg::CPhotoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPhotoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPhotoDlg)
	m_strId = _T("");
	m_strName = _T("");
	//}}AFX_DATA_INIT
}


void CPhotoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPhotoDlg)
	DDX_Control(pDX, IDC_EDIT_PHOTO_ID, m_ID);
	DDX_Text(pDX, IDC_EDIT_PHOTO_ID, m_strId);
	DDX_Text(pDX, IDC_EDIT_PHOTO_NAME, m_strName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPhotoDlg, CDialog)
	//{{AFX_MSG_MAP(CPhotoDlg)
	ON_BN_CLICKED(IDC_ADD_PHOTO_BTN, OnAddPhotoBtn)
	ON_BN_CLICKED(IDC_MODIFY_PHOTO_BTN, OnModifyPhotoBtn)
	ON_BN_CLICKED(IDC_DELETE_PHOTO_BTN, OnDeletePhotoBtn)
	ON_BN_CLICKED(IDC_EXECUTE_PHOTO_BTN, OnExecutePhotoBtn)
	ON_BN_CLICKED(IDC_LOOKFOR_PHOTO_BTN, OnLookforPhotoBtn)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_SEL_PHOTO_BTN, OnSelPhotoBtn)
	ON_EN_KILLFOCUS(IDC_EDIT_PHOTO_ID, OnKillfocusEditPhotoId)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPhotoDlg message handlers


BOOL CPhotoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_hPhotoBitmap = NULL;
	m_pBMPBuffer = NULL;

	CString str;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open("select * from StudentPhoto",_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);       	

	if(m_pRecordset->GetRecordCount()!=0)
	{
		m_pRecordset->MoveFirst();
		m_strId = m_pRecordset->GetCollect("ID").bstrVal;
		m_strName = m_pRecordset->GetCollect("Name").bstrVal;
		//--------图像数据----------------------//
		long lDataSize = m_pRecordset->GetFields()->GetItem("Photo")->ActualSize;
		if(lDataSize > 0)
		{
			_variant_t			varBLOB;
			varBLOB = m_pRecordset->GetFields()->GetItem("Photo")->GetChunk(lDataSize);
			if(varBLOB.vt == (VT_ARRAY | VT_UI1))
			{
				if(m_pBMPBuffer = new char[lDataSize+1])			
				{	
					char *pBuf = NULL;
					SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
					memcpy(m_pBMPBuffer,pBuf,lDataSize);				
					SafeArrayUnaccessData (varBLOB.parray);
					m_nFileLen = lDataSize;
					m_hPhotoBitmap = BufferToHBITMAP();					
				}
			}
		}
		//--------------------------------------//
	}	
	UpdateData(FALSE);	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

/////////////清除分配的对象/////////////
void CPhotoDlg::DestroyPhoto()
{
	if(m_hPhotoBitmap)
	{
		DeleteObject(m_hPhotoBitmap);
		m_hPhotoBitmap = NULL;
	}
	if(m_pBMPBuffer)
	{
		delete m_pBMPBuffer;
		m_pBMPBuffer = NULL;
	}
}

/////////////加载BMP文件到内存//////////////
BOOL CPhotoDlg::LoadBMPFile(const char *pBMPPathname)
{
	CFile file;
	if( !file.Open( pBMPPathname, CFile::modeRead) )
		return FALSE;
	m_nFileLen = file.GetLength();
	m_pBMPBuffer = new char[m_nFileLen + 1];
	if(!m_pBMPBuffer)
		return FALSE;
	if(file.ReadHuge(m_pBMPBuffer,m_nFileLen) != m_nFileLen)
		return FALSE;
	return TRUE;
}

///////////将内存中的BMP文件内容转换到HBITMAP///////
HBITMAP CPhotoDlg::BufferToHBITMAP()
{
	HBITMAP				hBmp;
	LPSTR				hDIB,lpBuffer = m_pBMPBuffer;
	LPVOID				lpDIBBits;
	BITMAPFILEHEADER	bmfHeader;
	DWORD				bmfHeaderLen;

	bmfHeaderLen = sizeof(bmfHeader);
	strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen);

	if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL;
	hDIB = lpBuffer + bmfHeaderLen;
	BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
	BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;

	lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;
	CClientDC dc(this);
	hBmp = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS);
	return hBmp;
}

//-----------在屏幕上显示图像-------------------//
void CPhotoDlg::DrawUserPhoto(int x, int y, CDC *pDC)
{
	if(!m_hPhotoBitmap) return;
	
	HBITMAP OldBitmap;

	CWnd *pWnd = GetDlgItem(IDC_PICTURE);
	CRect rect; 
	pWnd->GetClientRect(&rect);	
	pDC = pWnd->GetDC();

	CDC MemDC;
	
	MemDC.CreateCompatibleDC(pDC);
	OldBitmap=(HBITMAP)MemDC.SelectObject(m_hPhotoBitmap);
	pDC->BitBlt(x,y,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);
	MemDC.SelectObject(OldBitmap);
}
//-----------------------------------------//

void CPhotoDlg::OnAddPhotoBtn() 
{
	// TODO: Add your control notification handler code here
	if(theApp.m_Level != 2)
	{
		AfxMessageBox("您无权添加记录");
		return;
	}
	DestroyPhoto();
	m_strId = "";
	m_strName = "";
	AfxMessageBox("请选择照片并输入相关信息");
	action = 1;	
	Invalidate();
	UpdateData(FALSE);
}

void CPhotoDlg::OnModifyPhotoBtn() 
{
	// TODO: Add your control notification handler code here
	if(theApp.m_Level != 2)
	{
		AfxMessageBox("您无权修改记录");
		return;
	}
	if(AfxMessageBox("确定修改此照片吗?",MB_YESNO)==IDYES)	
	{
		DestroyPhoto();
		action = 2;	
		Invalidate();
	}
}

void CPhotoDlg::OnDeletePhotoBtn() 
{
	// TODO: Add your control notification handler code here
	if(theApp.m_Level != 2)
	{
		AfxMessageBox("您无权删除记录");
		return;
	}
	if(AfxMessageBox("确定删除此记录吗?",MB_YESNO)==IDYES)	
		action = 3;		
}

void CPhotoDlg::OnExecutePhotoBtn() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
	CString sql,str,id;
	sql = "select * from StudentPhoto ";
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);       
	
	char			*pBuf = m_pBMPBuffer;
	VARIANT			varBLOB;
	SAFEARRAY		*psa;
	SAFEARRAYBOUND	rgsabound[1];

	switch(action)
	{
	case 1:		
		m_pRecordset->AddNew();
		//学号
		m_pRecordset->PutCollect("ID",(_variant_t)m_strId);
		//姓名
		m_pRecordset->PutCollect("Name",(_variant_t)m_strName);
		//---------------图像数据-----------------------------//
		if(pBuf)
		{    
			rgsabound[0].lLbound = 0;
			rgsabound[0].cElements = m_nFileLen;
			psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
			for (long i = 0; i < (long)m_nFileLen; i++)
				SafeArrayPutElement (psa, &i, pBuf++);
			varBLOB.vt = VT_ARRAY | VT_UI1;
			varBLOB.parray = psa;
			m_pRecordset->GetFields()->GetItem("Photo")->AppendChunk(varBLOB);
		}
		
		//-----------------------------------------------------//
		m_pRecordset->Update();	
		break;

	case 2:	
		if(m_pRecordset->GetRecordCount()!=0)
		{
			m_pRecordset->MoveFirst();
			while(!m_pRecordset->adoEOF)
			{
				id=m_pRecordset->GetCollect("ID").bstrVal;
				if(id.CompareNoCase(m_strId) == 0)
				{
					//---------------图像数据-----------------------------//
					if(pBuf)
					{    
						rgsabound[0].lLbound = 0;
						rgsabound[0].cElements = m_nFileLen;
						psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
						for (long i = 0; i < (long)m_nFileLen; i++)
							SafeArrayPutElement (psa, &i, pBuf++);
						varBLOB.vt = VT_ARRAY | VT_UI1;
						varBLOB.parray = psa;
						m_pRecordset->GetFields()->GetItem("Photo")->AppendChunk(varBLOB);
					}
					
					//-----------------------------------------------------//				
				}
				m_pRecordset->MoveNext();			
			}
		}		
		break;
	case 3:
		if(m_pRecordset->GetRecordCount()!=0)
		{
			m_pRecordset->MoveFirst();
			while(!m_pRecordset->adoEOF)
			{
				id=m_pRecordset->GetCollect("ID").bstrVal;
				if(id.CompareNoCase(m_strId) == 0)			
					m_pRecordset->Delete(adAffectCurrent);					
				m_pRecordset->MoveNext();			
			}
		}		
		break;		
		
	default:
		break;
	}

	m_pRecordset->Close();
	UpdateData(FALSE);
	action = 0;	
	OnInitDialog();
}

void CPhotoDlg::OnLookforPhotoBtn() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	MessageBox("请在编号输入框中输入您要查找的学号","照片管理");
    m_ID.SetFocus();
    action =4;	
	UpdateData(FALSE);
}



void CPhotoDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CWnd *pWnd = GetDlgItem(IDC_PICTURE);
	CRect rect; 
	pWnd->GetClientRect(&rect);
	DrawUserPhoto(rect.left,rect.top,&dc);
	CDialog::OnPaint();	
	// Do not call CDialog::OnPaint() for painting messages
}

void CPhotoDlg::OnSelPhotoBtn() 
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*||";
	CFileDialog dlg(TRUE,"BMP",NULL,  OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	if(dlg.DoModal() == IDOK)
	{
		CString pathname = dlg.GetPathName();
		DestroyPhoto();
		LoadBMPFile(pathname);
		m_hPhotoBitmap = BufferToHBITMAP();
		Invalidate();
	}		
}


void CPhotoDlg::OnKillfocusEditPhotoId() 
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);

	CString sql,str;
	sql = "select * from StudentPhoto where ID = '"+m_strId+"'";
	m_pRecordset.CreateInstance("ADODB.Recordset");
	m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);      

	if(m_pRecordset->GetRecordCount()!=0)
	{
		m_strName = m_pRecordset->GetCollect("Name").bstrVal;
		//--------图像数据----------------------//
		long lDataSize = m_pRecordset->GetFields()->GetItem("Photo")->ActualSize;
		if(lDataSize > 0)
		{
			_variant_t			varBLOB;
			varBLOB = m_pRecordset->GetFields()->GetItem("Photo")->GetChunk(lDataSize);
			if(varBLOB.vt == (VT_ARRAY | VT_UI1))
			{
				if(m_pBMPBuffer = new char[lDataSize+1])			
				{	
					char *pBuf = NULL;
					SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
					memcpy(m_pBMPBuffer,pBuf,lDataSize);				
					SafeArrayUnaccessData (varBLOB.parray);
					m_nFileLen = lDataSize;
					m_hPhotoBitmap = BufferToHBITMAP();					
				}
			}
		}
		//--------------------------------------//
	}
	else
	{
		if (action ==4)
		{
			MessageBox("您要查找的学号照片信息不存在","照片管理");
		}
	    m_strId="";
		m_strName="";
	}
	action=0;
	Invalidate();
	m_pRecordset->Close();
	UpdateData(false);
}